package com.ttl.bc.ar.reqconfirm.db;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ttl.bc.ap.stkrequest.BCStkRequestBean;
import com.ttl.bc.ap.stkrequest.BCStkRequestDAO;
import com.ttl.bc.ap.stkrequest.BCStkRequestForm;
import com.ttl.bc.ap.stkrequest.BCStkRequestShowBean;
import com.ttl.bc.ap.stkrequest.BCStkRequestSubBean;

import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmBean;
import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmShowBean;
import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmSubBean;
import com.ttl.bc.ic.stkissue.bean.BCStkIssueBean;
import com.ttl.bc.ic.stkissue.bean.BCStkIssueSubBean;
import com.ttl.bc.ic.stkissue.db.BCStkIssueDAO;

import com.ttl.common.util.CommonUtils;

public class BCReqConfirmBO {

	@SuppressWarnings("unchecked")
	public ArrayList find(HttpServletRequest request) {
		
		String userName = (String)request.getSession().getAttribute("userName");			
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");
				
		if(monthFind==null) {			
			monthFind = CommonUtils.getCurrentDateFormat("MM");										
		}
		
		if(yearFind==null) {
			yearFind = CommonUtils.getCurrentDateFormat("yyyyy");
			
			if (Integer.parseInt(yearFind)<2500) {								
				yearFind = (""+(Integer.parseInt(yearFind)+543)).substring(2);			
			}else {
				yearFind = yearFind.substring(2);
			}			
		}
				
		request.getSession().setAttribute("monthFind", monthFind);
		request.getSession().setAttribute("yearFind", yearFind);
		
		String hql = "FROM BCReqConfirmBean s  " +
					 "WHERE upper(s.creatorCode)='"+userName.toUpperCase()+"' " +
					 "and s.docNo like '%"+yearFind+monthFind+"%' "+
					 "and s.isCancel =0 "+
					 "ORDER BY s.docNo desc";								
//		System.out.println(hql);
		BCReqConfirmDAO dao = new BCReqConfirmDAO();
		List results = dao.find(hql);

		if(results!=null) {			
			ArrayList<BCReqConfirmShowBean> al = new ArrayList<BCReqConfirmShowBean>();
			
			for(int i=0; i<results.size(); i++) {
				BCReqConfirmBean s2 = (BCReqConfirmBean)results.get(i);
				
				for(Object o: s2.getBcReqConfirmSubs()) {
					BCReqConfirmSubBean sub2 = (BCReqConfirmSubBean)o; 			
					
					BCReqConfirmShowBean bean = new BCReqConfirmShowBean();
					bean.setDocNo(s2.getDocNo());
					bean.setDocDate(s2.getDocDate());
					bean.setMyDescription(s2.getMyDescription());
					bean.setBillStatus(s2.getBillStatus());					
					bean.setItemCode(sub2.getItemCode());
					bean.setItemName(sub2.getItemName());					
					bean.setQty(sub2.getQty());					
					bean.setIttl_unitName(sub2.getIttl_unitName());
					bean.setPrice(sub2.getPrice());
					bean.setAmount(sub2.getAmount());
					bean.setMode("");
					bean.setIttl_status(s2.getIttl_status());
					al.add(bean);
				}				
			}
			results = al;
		}
		
		return (ArrayList)results;
	}

	@SuppressWarnings("unchecked")
	public ArrayList findAll(HttpServletRequest request) {
		
		@SuppressWarnings("unused")
		String userName = (String)request.getSession().getAttribute("userName");			
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");
		String docNoFind = (String)request.getParameter("docNoFind");
		
		
		String hql = "";
		if(monthFind==null || yearFind==null) {
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("MM");			
			monthFind = sdf.format(date);
			
			sdf = new SimpleDateFormat("yyyy");
			String yy="";
			if (Integer.parseInt(sdf.format(date))<2500) {
				yy = (""+Integer.parseInt(sdf.format(date))+543).substring(2,2);
			}else {
				yy = sdf.format(date).substring(2,2);
			}			
			yearFind = yy;					
		}
		
		hql = "FROM BCStkRequestBean s  " +
		 		"WHERE s.docNo like '%"+yearFind+monthFind+"%' "+
		 		"and s.isCancel =0 "+			 		 
		 		"ORDER BY s.docNo desc ";		
		
		if(docNoFind!=null) {
			request.getSession().setAttribute("docNoFind", docNoFind);
			
			hql = "FROM BCStkRequestBean s  " +
						 "WHERE s.docNo like '%"+docNoFind+"%' "+
						 "and s.isCancel =0 "+
						 "ORDER BY s.docNo desc ";
		}		
		
		//System.out.println("######hql = "+hql);
		BCReqConfirmDAO dao = new BCReqConfirmDAO();
		List results = dao.find(hql);

		if(results!=null) {			
			ArrayList<BCStkRequestShowBean> al = new ArrayList<BCStkRequestShowBean>();
			
			for(int i=0; i<results.size(); i++) {
				BCStkRequestBean s2 = (BCStkRequestBean)results.get(i);
				
				for(Object o: s2.getBcStkRequestSubs()) {
					BCStkRequestSubBean sub2 = (BCStkRequestSubBean)o; 			
					
					BCStkRequestShowBean bean = new BCStkRequestShowBean();
					bean.setDocNo(s2.getDocNo());
					bean.setDocDate(s2.getDocDate());
					bean.setMyDescription(s2.getMyDescription());
					bean.setBillStatus(s2.getBillStatus());					
					bean.setItemCode(sub2.getItemCode());
					bean.setItemName(sub2.getItemName());					
					bean.setQty(sub2.getQty());					
					bean.setPrice(sub2.getPrice());
					bean.setAmount(sub2.getAmount());					
					bean.setIttl_unitName(sub2.getIttl_unitName());					
					bean.setIttl_status(s2.getIttl_status());
					bean.setMode("");
					
					al.add(bean);
				}				
			}
			results = al;
		}
		
		return (ArrayList)results;
	}

	@SuppressWarnings("unchecked")
	public BCStkRequestForm approveShow(HttpServletRequest request,
			HttpServletResponse response) {
				
		BCStkRequestForm form = new BCStkRequestForm();
		
		String docNo = (String)request.getParameter("docNo");
		System.out.println("#docNo :"+docNo);
		
		BCStkRequestDAO dao = new BCStkRequestDAO();	
		String hql = "FROM BCStkRequestBean h " +
				" WHERE upper(h.docNo) = '"+docNo.toUpperCase()+"' ";
				
		List results = dao.find(hql);
		if (results!=null && results.size()>0) {
			
			for(int i=0; i<results.size(); i++) {				
				BCStkRequestBean s2 = (BCStkRequestBean)results.get(i);
								
				form.setDocNo(docNo);
				System.out.println("####### > "+form.getDocNo());
				form.setDocDate(""+s2.getDocDate());
				form.setDepartCode(s2.getDepartCode());
				form.setProjectCode(s2.getProjectCode());
				form.setCreatorCode(s2.getCreatorCode());
				form.setiTTL_deptRequest(s2.getIttl_deptRequest());
				form.setMyDescription(s2.getMyDescription());
																																
				int id = 0;
				
				for(Object o: s2.getBcStkRequestSubs()) {
					BCStkRequestSubBean sub2 = (BCStkRequestSubBean)o;
					id = sub2.getLineNumber();					
					switch (id) {
					case 1:						
						form.setItemCode1(sub2.getItemCode());
						form.setItemName1(sub2.getItemName());
						form.setShortName1(sub2.getIttl_itemCode_ttl2());
//						form.setUnitCode1(sub2.getBcitemUnit().getCode());
//						form.setUnitName1(sub2.getBcitemUnit().getName());
						form.setUnitCode1(sub2.getUnitCode());
						
						form.setQty1(""+sub2.getQty());						
						form.setDivision1(sub2.getIttl_division());						
						form.setIttl_unitName1(sub2.getIttl_unitName());						
						break;
					case 2:
						form.setItemCode2(sub2.getItemCode());
						form.setItemName2(sub2.getItemName());
						form.setShortName2(sub2.getIttl_itemCode_ttl2());
//						form.setUnitCode2(sub2.getBcitemUnit().getCode());
//						form.setUnitName2(sub2.getBcitemUnit().getName());
						form.setUnitCode2(sub2.getUnitCode());
						
						form.setQty2(""+sub2.getQty());						
						form.setDivision2(sub2.getIttl_division());
						form.setIttl_unitName2(sub2.getIttl_unitName());						
						break;
					case 3:
						form.setItemCode3(sub2.getItemCode());
						form.setItemName3(sub2.getItemName());
						form.setShortName3(sub2.getIttl_itemCode_ttl2());
//						form.setUnitCode3(sub2.getBcitemUnit().getCode());
//						form.setUnitName3(sub2.getBcitemUnit().getName());
						form.setUnitCode3(sub2.getUnitCode());
						
						form.setQty3(""+sub2.getQty());						
						form.setDivision3(sub2.getIttl_division());
						form.setIttl_unitName3(sub2.getIttl_unitName());
						
						break;		
					case 4:
						form.setItemCode4(sub2.getItemCode());
						form.setItemName4(sub2.getItemName());
						form.setShortName4(sub2.getIttl_itemCode_ttl2());
//						form.setUnitCode4(sub2.getBcitemUnit().getCode());
//						form.setUnitName4(sub2.getBcitemUnit().getName());
						form.setUnitCode4(sub2.getUnitCode());
						form.setQty4(""+sub2.getQty());						
						form.setDivision4(sub2.getIttl_division());
						form.setIttl_unitName4(sub2.getIttl_unitName());
						
						break;						
					}	
				}				
			}			
		}	
		return form;
	}

	@SuppressWarnings("unchecked")
	public boolean approveConfirm(HttpServletRequest request,
			HttpServletResponse response) {				
		
		BCStkRequestForm reqForm = setForm(request);		
		request.setAttribute("docNoFind", reqForm.getDocNo().substring(9));
		
		BCStkRequestBean  stkReq = setRequestObject(reqForm);
		stkReq.setIttl_status("3");		//ใส่ ittl_status เป็น 3 เมื่อจะจ่ายของซื้อนั้น					
		Iterator it = stkReq.getBcStkRequestSubs().iterator();
		while(it.hasNext()) {
			BCStkRequestSubBean sub = (BCStkRequestSubBean)it.next();
			sub.setIttl_status("3");
		}
				
		BCStkRequestDAO dao = new BCStkRequestDAO();
		dao.update(stkReq.getDocNo(),stkReq);
		 
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :update");
		
		BCReqConfirmBean reqComfirm = setReqConfirm(stkReq);
		reqComfirm.setDocNo(stkReq.getDocNo());
		reqComfirm.setIttl_status("3");		//ใส่ ittl_status เป็น 3 เมื่อจะจ่ายของซื้อนั้น
		it = reqComfirm.getBcReqConfirmSubs().iterator();
		while(it.hasNext()) {
			BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
			sub.setIttl_status("3");
		}		
		
		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
		dao2.update(reqComfirm.getDocNo(), reqComfirm);
				
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :update");	
					
		// สร้างรายการใหม่ลงใน table: BCStkIssue  (เพื่อเก็บไว้ออก report)
		BCStkIssueBean issue = setBCStkIssue(request,stkReq);
		BCStkIssueDAO dao3 = new BCStkIssueDAO();
		System.out.println("add stkissue: "+dao3.add(issue));

		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueDAO #mode :add "+
				  "#docNo:"+issue.getDocNo());
		
		// ไม่ต้องใส่ข้อมูล itemCode รายการนั้นใน table: processStock เพราะเป็นของซื้อ 
         
		return true;
	}

	@SuppressWarnings("unchecked")
	private BCStkIssueBean setBCStkIssue(HttpServletRequest request,
			BCStkRequestBean stkReq) {
		BCStkIssueBean issue = new BCStkIssueBean();
		
		issue.setDocNo((String)request.getParameter("stkIssueDocNo"));
		issue.setDocDate(CommonUtils.convertStringToDate((String)request.getParameter("stkIssueDocDate")));		
		issue.setDepartCode(stkReq.getDepartCode());		
				
		try {
			issue.setMyDescription(new String((""+(String)request.getParameter("stkIssueMyDescription")).getBytes("ISO8859_1"),"UTF-8"));							
		}catch (Exception e) {
			
		}
		issue.setMyDescription(issue.getMyDescription()+","+stkReq.getDocNo());
		
		issue.setPersonCode((String)request.getParameter("stkIssuePersonCode"));				
		
		issue.setSumOfAmount(new Float(0));
		
		issue.setGlFormat("");
		issue.setGlStartPosting(0);
		
		issue.setIsCompleteSave(1);
		issue.setIsConfirm(0);
		issue.setIsCancel(0);
		
		issue.setProjectCode(stkReq.getProjectCode());
		issue.setCreatorCode((String)request.getParameter("stkIssuePersonCode"));				
		issue.setCreateDateTime(new Date());								
		
		HashSet s = new HashSet();		
		
		Iterator it =   stkReq.getBcStkRequestSubs().iterator();
	    while(it.hasNext()) {
	    	BCStkRequestSubBean sub2 = (BCStkRequestSubBean)it.next();
	    	BCStkIssueSubBean sub = new BCStkIssueSubBean();
	    	sub.setItemCode(sub2.getItemCode());
	    	sub.setDocDate(issue.getDocDate());
	    	sub.setDepartCode(sub2.getDepartCode());
	    	sub.setPersonCode(issue.getPersonCode());
	    	
	    	sub.setQty(sub2.getQty());
	    	sub.setRetQty(sub2.getRemainQty());
	    	sub.setProjectCode(sub2.getProjectCode());
	    	sub.setLineNumber(sub2.getLineNumber());
	    	sub.setItemName(sub2.getItemName());
	    	sub.setRefNo(stkReq.getDocNo());
	    	
//	    	BCItemUnitBean unit = new BCItemUnitBean();
//	    	unit.setCode(sub2.getBcitemUnit().getCode());
//	    	sub.setBcitemUnit(unit);	    	
	    	sub.setUnitCode(sub2.getUnitCode());
	    	
	    	sub.setBcStkIssue(issue);
	    	
	    	s.add(sub);
	    }		
		issue.setBcStkIssueSubs(s);
		
		return issue;
	}

	@SuppressWarnings("unchecked")
	public static BCReqConfirmBean setReqConfirm(BCStkRequestBean stkReq) {
		BCReqConfirmBean cf = new BCReqConfirmBean();
		
		cf.setDocNo(stkReq.getDocNo());
		cf.setDocDate(stkReq.getDocDate());
		cf.setCreatorCode(stkReq.getCreatorCode());
		cf.setCreateDateTime(stkReq.getCreateDateTime());
		cf.setIsCancel(0);
		cf.setIsConfirm(0);
		cf.setBillStatus(0);
		cf.setConfirmName(stkReq.getCreatorCode());
		cf.setMyDescription(stkReq.getMyDescription());
		
		HashSet h = new HashSet();
    	Iterator it = stkReq.getBcStkRequestSubs().iterator();
    	
        while(it.hasNext()) {
        	BCStkRequestSubBean sub = (BCStkRequestSubBean)it.next();
        	
        	BCReqConfirmSubBean cfSub = new BCReqConfirmSubBean();
        	cfSub.setIttl_status("3");
        	cfSub.setDocDate(sub.getDocDate());
        	cfSub.setItemCode(sub.getItemCode());
        	cfSub.setStkRequestNo(stkReq.getDocNo());
        	cfSub.setDepartCode(sub.getDepartCode());
        	cfSub.setQty(sub.getQty());
        	cfSub.setConfirmQty(sub.getQty());
        	cfSub.setRemainQty(sub.getRemainQty());
//        	cfSub.setUnitCode(sub.getBcitemUnit().getCode());
        	cfSub.setUnitCode(sub.getUnitCode());
        	cfSub.setLineNumber(sub.getLineNumber());
        	cfSub.setItemName(sub.getItemName());
        	cfSub.setCreatorCode(sub.getCreatorCode());
        	cfSub.setCreateDateTime(sub.getCreateDateTime());
        	cfSub.setIttl_unitName(sub.getIttl_unitName());
        	cfSub.setBcReqConfirm(cf);
        	        	
        	h.add(cfSub);
        }
        cf.setBcReqConfirmSubs(h);
        
        return cf;
	}

	@SuppressWarnings("unchecked")
	public static BCStkRequestBean setRequestObject(BCStkRequestForm reqForm) {
		
		BCStkRequestBean issue2 = new BCStkRequestBean();
						
		issue2.setBillStatus(1);
		issue2.setIsConfirm(1);
		issue2.setIsCancel(0);
				
		issue2.setDocNo(reqForm.getDocNo());
		issue2.setCreatorCode(reqForm.getCreatorCode());
		issue2.setDepartCode(reqForm.getDepartCode());
					
	    issue2.setDocDate(CommonUtils.convertStringToDate(reqForm.getDocDate()));
	    issue2.setCreateDateTime(new Date());
		
		issue2.setIttl_deptRequest(reqForm.getiTTL_deptRequest());
		issue2.setProjectCode(reqForm.getProjectCode());		
		
		issue2.setMyDescription(reqForm.getMyDescription());
				
		HashSet s = new HashSet();			
				
		if( reqForm.getItemCode1() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode1());
			issueSub2.setItemName(reqForm.getItemName1());
			issueSub2.setQty(Float.valueOf(reqForm.getQty1()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty1()));
					
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(reqForm.getUnitCode1());
//			issueSub2.setBcitemUnit(itemU);
			issueSub2.setUnitCode(reqForm.getUnitCode1());
			
			issueSub2.setLineNumber(1);
			issueSub2.setIttl_unitName(reqForm.getUnitName1());
			issueSub2.setIttl_division(reqForm.getDivision1());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());			
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}
		
		if(reqForm.getItemCode2() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode2());
			issueSub2.setItemName(reqForm.getItemName2());
			issueSub2.setQty(Float.valueOf(reqForm.getQty2()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty2()));
					
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(reqForm.getUnitCode2());
//			issueSub2.setBcitemUnit(itemU);		
			issueSub2.setUnitCode(reqForm.getUnitCode2());
			
			issueSub2.setLineNumber(2);
			issueSub2.setIttl_unitName(reqForm.getUnitName2());
			issueSub2.setIttl_division(reqForm.getDivision2());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());			
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}		
		
		if( reqForm.getItemCode3() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode3());
			issueSub2.setItemName(reqForm.getItemName3());
			issueSub2.setQty(Float.valueOf(reqForm.getQty3()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty3()));
					
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(reqForm.getUnitCode3());
//			issueSub2.setBcitemUnit(itemU);
			issueSub2.setUnitCode(reqForm.getUnitCode3());
			
			issueSub2.setLineNumber(3);
			issueSub2.setIttl_unitName(reqForm.getUnitName3());
			issueSub2.setIttl_division(reqForm.getDivision3());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());			
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}		
		
		if(reqForm.getItemCode4() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode4());
			issueSub2.setItemName(reqForm.getItemName4());
			issueSub2.setQty(Float.valueOf(reqForm.getQty4()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty4()));
					
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(reqForm.getUnitCode4());
//			issueSub2.setBcitemUnit(itemU);	
			issueSub2.setUnitCode(reqForm.getUnitCode4());
			
			issueSub2.setLineNumber(4);
			issueSub2.setIttl_unitName(reqForm.getUnitName2());
			issueSub2.setIttl_division(reqForm.getDivision2());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());			
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}
					
		issue2.setBcStkRequestSubs(s);
		return issue2;
	}

	@SuppressWarnings({ "unchecked", "unused" })
	private BCStkRequestBean setBCStkRequest(BCStkRequestForm reqForm) {
		
		BCStkRequestBean issue2 = new BCStkRequestBean();
		
		issue2.setBillStatus(1);
		issue2.setIsConfirm(1);
		issue2.setIsCancel(0);
				
		issue2.setDocNo(reqForm.getDocNo());
		issue2.setCreatorCode(reqForm.getCreatorCode());
		issue2.setDepartCode(reqForm.getDepartCode());
					
	    issue2.setDocDate(CommonUtils.convertStringToDate(reqForm.getDocDate()));
	    issue2.setCreateDateTime(new Date());
		
		issue2.setIttl_deptRequest(reqForm.getiTTL_deptRequest());
		issue2.setProjectCode(reqForm.getProjectCode());		
		
		issue2.setMyDescription(reqForm.getMyDescription());
				
		HashSet s = new HashSet();			
				
		if( reqForm.getItemCode1() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode1());
			issueSub2.setItemName(reqForm.getItemName1());
			issueSub2.setQty(Float.valueOf(reqForm.getQty1()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty1()));
					
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(reqForm.getUnitCode1());
//			issueSub2.setBcitemUnit(itemU);
			issueSub2.setUnitCode(reqForm.getUnitCode1());
			
			issueSub2.setLineNumber(1);
			issueSub2.setIttl_unitName(reqForm.getUnitName1());
			issueSub2.setIttl_division(reqForm.getDivision1());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());			
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}
		
		if(reqForm.getItemCode2() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode2());
			issueSub2.setItemName(reqForm.getItemName2());
			issueSub2.setQty(Float.valueOf(reqForm.getQty2()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty2()));
					
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(reqForm.getUnitCode2());
//			issueSub2.setBcitemUnit(itemU);					
			issueSub2.setUnitCode(reqForm.getUnitCode2());
			
			issueSub2.setLineNumber(2);
			issueSub2.setIttl_unitName(reqForm.getUnitName2());
			issueSub2.setIttl_division(reqForm.getDivision2());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());			
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}		
		
		if( reqForm.getItemCode3() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode3());
			issueSub2.setItemName(reqForm.getItemName3());
			issueSub2.setQty(Float.valueOf(reqForm.getQty3()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty3()));
					
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(reqForm.getUnitCode3());
//			issueSub2.setBcitemUnit(itemU);
			issueSub2.setUnitCode(reqForm.getUnitCode3());
			
			issueSub2.setLineNumber(3);
			issueSub2.setIttl_unitName(reqForm.getUnitName3());
			issueSub2.setIttl_division(reqForm.getDivision3());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());			
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}		
		
		if(reqForm.getItemCode4() != null) {
			BCStkRequestSubBean issueSub2 = new BCStkRequestSubBean();
			
			issueSub2.setDepartCode(reqForm.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());							
			issueSub2.setItemCode(reqForm.getItemCode4());
			issueSub2.setItemName(reqForm.getItemName4());
			issueSub2.setQty(Float.valueOf(reqForm.getQty4()));
			issueSub2.setRemainQty(Float.valueOf(reqForm.getQty4()));
					
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(reqForm.getUnitCode4());
//			issueSub2.setBcitemUnit(itemU);
			issueSub2.setUnitCode(reqForm.getUnitCode4());
			
			issueSub2.setLineNumber(4);
			issueSub2.setIttl_unitName(reqForm.getUnitName2());
			issueSub2.setIttl_division(reqForm.getDivision2());								
			
			issueSub2.setProjectCode(reqForm.getProjectCode());
			issueSub2.setCreatorCode(reqForm.getCreatorCode());
			issueSub2.setCreateDateTime(issue2.getCreateDateTime());			
			issueSub2.setBcStkRequest(issue2);
						
			s.add(issueSub2);
		}
					
		issue2.setBcStkRequestSubs(s);
		return issue2;
	}

	public static BCStkRequestForm setForm(HttpServletRequest request) {
		BCStkRequestForm stkReqForm = new BCStkRequestForm();
		
		stkReqForm.setDocNo(request.getParameter("docNo"));
		stkReqForm.setDocDate(request.getParameter("docDate"));
		
		stkReqForm.setProjectCode(request.getParameter("projectCode"));
		stkReqForm.setCreatorCode(request.getParameter("creatorCode"));
		
		stkReqForm.setiTTL_deptRequest(request.getParameter("iTTL_deptRequest"));
		try {

			stkReqForm.setMyDescription(new String((""+(String)request.getParameter("myDescription")).getBytes("ISO8859_1"),"UTF-8"));
						
		} catch(Exception ex){}
		try {	
			stkReqForm.setItemName1(new String((""+(String)request.getParameter("itemName1")).getBytes("ISO8859_1"),"UTF-8"));
			stkReqForm.setUnitName1(new String((""+(String)request.getParameter("unitName1")).getBytes("ISO8859_1"),"UTF-8"));
			stkReqForm.setDivision1(new String((""+(String)request.getParameter("division1")).getBytes("ISO8859_1"),"UTF-8"));
		} catch(Exception ex){}
		try {	
			stkReqForm.setItemName2(new String((""+(String)request.getParameter("itemName2")).getBytes("ISO8859_1"),"UTF-8"));
			stkReqForm.setUnitName2(new String((""+(String)request.getParameter("unitName2")).getBytes("ISO8859_1"),"UTF-8"));
			stkReqForm.setDivision2(new String((""+(String)request.getParameter("division2")).getBytes("ISO8859_1"),"UTF-8"));
		} catch(Exception ex){}
		try {	
			stkReqForm.setItemName3(new String((""+(String)request.getParameter("itemName3")).getBytes("ISO8859_1"),"UTF-8"));
			stkReqForm.setUnitName3(new String((""+(String)request.getParameter("unitName3")).getBytes("ISO8859_1"),"UTF-8"));
			stkReqForm.setDivision3(new String((""+(String)request.getParameter("division3")).getBytes("ISO8859_1"),"UTF-8"));
		} catch(Exception ex){}
		try {	
			stkReqForm.setItemName4(new String((""+(String)request.getParameter("itemName4")).getBytes("ISO8859_1"),"UTF-8"));
			stkReqForm.setUnitName4(new String((""+(String)request.getParameter("unitName4")).getBytes("ISO8859_1"),"UTF-8"));
			stkReqForm.setDivision4(new String((""+(String)request.getParameter("division4")).getBytes("ISO8859_1"),"UTF-8"));			
															
		}catch (Exception e) {
			System.out.println("############ error:"+e.toString());
		}				
		
		stkReqForm.setDepartCode(request.getParameter("departCode"))	;
		
		//System.out.println("request.getParameter(departCode) = "+request.getParameter("departCode"));
		
		stkReqForm.setIssueType(request.getParameter("issueType"));						
						
		stkReqForm.setItemCode1(request.getParameter("itemCode1"));	
		stkReqForm.setUnitCode1(request.getParameter("unitCode1"));
		stkReqForm.setQty1(request.getParameter("qty1"));				
		stkReqForm.setPrice1(request.getParameter("price1"));
		stkReqForm.setAmount1(request.getParameter("amount1"));		
		
		stkReqForm.setItemCode2(request.getParameter("itemCode2"));		
		stkReqForm.setUnitCode2(request.getParameter("unitCode2"));
		stkReqForm.setQty2(request.getParameter("qty2"));		
		stkReqForm.setPrice2(request.getParameter("price2"));
		stkReqForm.setAmount2(request.getParameter("amount2"));
		
		stkReqForm.setItemCode3(request.getParameter("itemCode3"));		
		stkReqForm.setUnitCode3(request.getParameter("unitCode3"));
		stkReqForm.setQty3(request.getParameter("qty3"));				
		stkReqForm.setPrice3(request.getParameter("price3"));
		stkReqForm.setAmount3(request.getParameter("amount3"));
		
		stkReqForm.setItemCode4(request.getParameter("itemCode4"));
		stkReqForm.setUnitCode4(request.getParameter("unitCode4"));
		stkReqForm.setQty4(request.getParameter("qty4"));	
		stkReqForm.setPrice4(request.getParameter("price4"));
		stkReqForm.setAmount4(request.getParameter("amount4"));		
									
			
		return stkReqForm;
	}

	@SuppressWarnings("unchecked")
	public boolean approveConfirmCancel(HttpServletRequest request,
			HttpServletResponse response) {
		
		BCStkRequestForm reqForm = setForm(request);				
		BCStkRequestBean  stkReq = setRequestObject(reqForm);
		stkReq.setIttl_status("2");		//ใส่ ittl_status เป็น 2 เมื่อจะยกเลิกการจ่ายของซื้อนั้น		
		Iterator it = stkReq.getBcStkRequestSubs().iterator();
		while(it.hasNext()) {
			BCStkRequestSubBean sub = (BCStkRequestSubBean)it.next();
			sub.setIttl_status("2");
		}				
		
		BCStkRequestDAO dao = new BCStkRequestDAO();
		dao.update(stkReq.getDocNo(),stkReq);
		 
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkRequestDAO #mode :update");
		
		BCReqConfirmBean reqComfirm = setReqConfirm(stkReq);
		reqComfirm.setDocNo(stkReq.getDocNo());
		//ใส่ ittl_status เป็น 2 เมื่อจะยกเลิกการจ่ายของซื้อนั้น
		reqComfirm.setIttl_status("2");
		it = reqComfirm.getBcReqConfirmSubs().iterator();
		while(it.hasNext()) {
			BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
			sub.setIttl_status("2");
		}
				
		BCReqConfirmDAO dao2 = new BCReqConfirmDAO();
		dao2.update(reqComfirm.getDocNo(), reqComfirm);
				
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmDAO #mode :update");	
				
		// ลบรายการออกจาก table: BCStkIssue
		String hql = "FROM BCStkIssueBean s WHERE docNo='"+stkReq.getDocNo()+"'";				
		BCStkIssueDAO dao3 = new BCStkIssueDAO();
		dao3.delete(hql);
		
		System.out.println("delete stkissue: ");

		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueDAO #mode :delete "+
				  "#docNo:"+stkReq.getDocNo());
		
		// ไม่ต้องใส่ข้อมูล itemCode รายการนั้นใน table: processStock เพราะเป็นของซื้อ 
         
		return true;
	}

}
